// pages/shopOrders/orderDetail/orderDetail.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: null,
        orderId: null,
        orderInfo: null,
        loginsticNum: null,
        logicsticName: null,
        showLogistic: false,
        selectGoods: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            orderId: options.orderId
        })
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
        let pages = getCurrentPages(); //获取当前页面实例
        let prepage = pages[pages.length - 2]
        if (prepage == undefined || prepage == null) return;
        prepage.updateData()
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getUserInfo: function () {
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function (res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    userInfo: res
                })
                that.getOrderInfo()
            }
        }).catch(function (e) { })
    },

    getOrderInfo: function() {
        const that = this
        let data = {
            order_id: this.data.orderId,
            get_user: 1,
            paramter: 1
        }
        common.post('Orderlist/user_order', data).then(function(res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    orderInfo: res[0]
                })
            }
        }).catch(function(e) {})
    },

    logicsticNameInput: function(e) {
        this.setData({
            logicsticName: e.detail.value
        })
    },

    loginsticNumInput: function(e) {
        this.setData({
            loginsticNum: e.detail.value
        })
    },

    checkboxChange: function(e) {
        this.setData({
            selectGoods: e.detail.value
        })
    },

    myCatchTouch: function() {
        return
    },

    closeShowLogistic: function() {
        this.setData({
            loginsticNum: null,
            logicsticName: null,
            showLogistic: false
        })
    },

    goSend: function(e) {
        if (this.data.selectGoods.length == 0) {
            wx.showToast({
                title: '请选择订单商品',
                icon: 'none',
                duration: 2000
            })
            return
        }
        this.setData({
            loginsticNum: null,
            logicsticName: null,
            showLogistic: true
        })
    },

    sureSend: function() {
        wx.showLoading({
            title: '请稍后'
        })
        const that = this
        let data = {
            order_id: this.data.orderId,
            rec_id: this.data.selectGoods,
            express_sn: this.data.loginsticNum,
            expressCompany: this.data.logicsticName
        }
        common.post('Orderlist/send_order', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {}
                })
                return
            }
            wx.showModal({
                title: '提示',
                content: '发货成功',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            orderInfo: null,
                            loginsticNum: null,
                            logicsticName: null,
                            showLogistic: false,
                            selectGoods: []
                        })
                        that.getOrderInfo()
                    }
                }
            })
        }).catch(function(e) {})
    },

    goGoodsDetail: function(e) {
        wx.navigateTo({
            url: '/pages/goods/goodsDetail/goodsDetail?goodsId=' + e.currentTarget.dataset.id
        })
    }
})